﻿
using System ;
using System.Collections.Generic ;

using MskEdit.Sdk ;


namespace MskEdit.Dxf.Model
{

    public class DxfDocument
    {
        string          name_ ;
        
        DxfHeader       dxfHeader_ ;
        DxfClasses      dxfClasses_ ;
        DxfTables       dxfTables_ ;
        DxfBlocks       dxfBlocks_ ;
        DxfEntities     dxfEntities_ ;
        DxfObjects      dxfObjects_ ;

        DxfSection[]    dxfSections_ ;
       

        private DxfDocument( string name, DxfSection[] dxfSections )
        {
            name_           = name ;
            dxfHeader_      = null ;
            
            dxfSections_    = dxfSections ;
            
            foreach ( var section in dxfSections_ ) {
            
                if ( dxfHeader_ == null )
                    dxfHeader_ = section.TryGetHeader( null ) ;
            
                if ( dxfClasses_ == null )
                    dxfClasses_ = section.TryGetClasses( null ) ;
            
                if ( dxfTables_ == null )
                    dxfTables_ = section.TryGetTables( null ) ;
            
                if ( dxfBlocks_ == null )
                    dxfBlocks_ = section.TryGetBlocks( null ) ;
            
                if ( dxfEntities_ == null )
                    dxfEntities_ = section.TryGetEntities( null ) ;
            
                if ( dxfObjects_ == null )
                    dxfObjects_ = section.TryGetObjects( null ) ;
            }
        }
        
        
        public static DxfDocument TryFrom( string name, string[] allLines, DxfDocument fallbackValue ) {
        
            int internalLineNo = 0;
            
            List<DxfSection> dxfSections = new List<DxfSection>() ;
            DxfSection dxfSection = null ;
            
            do {
                dxfSection = DxfSection.TryFrom( allLines, ref internalLineNo, null ) ;
                
                if ( dxfSection != null )
                    dxfSections.Add( dxfSection ) ;
                
            } while ( dxfSection != null ) ;
            
            if ( dxfSections.Count == 0 )
                return fallbackValue ;
                        
            return new DxfDocument( name, dxfSections.ToArray() ) ;
        }
        
        
        public string TryGetAttrdefPrompt( string blockTag, string attribTag, string fallbackValue ) {
        
            if ( dxfBlocks_ == null )
                return fallbackValue ;
                
            return dxfBlocks_.TryGetAttrdefPrompt( blockTag, attribTag, fallbackValue ) ;
        }
        
        
        public string TryGetAttribText( string attribTag, string fallbackValue ) {
        
            if ( dxfEntities_ == null )
                return fallbackValue ;
                
            return dxfEntities_.TryGetAttribText( attribTag, fallbackValue ) ;
        }
        
        
        public string Name {
            get { return name_ ; }
        }
        
        
        public DxfSection[] Sections {
            get { return dxfSections_ ; }
        }
        
        
        public DxfHeader Header {
            get { return dxfHeader_ ; }
        }
        
        
        public DxfClasses Classes {
            get { return dxfClasses_ ; }
        }
        
        
        public DxfTables Tables {
            get { return dxfTables_ ; }
        }
        
        
        public DxfBlocks Blocks {
            get { return dxfBlocks_ ; }
        }
        
        
        public DxfEntities Entities {
            get { return dxfEntities_ ; }
        }
        
        
        public DxfObjects Objects {
            get { return dxfObjects_ ; }
        }

    }
}
